/**
 * Created by liujiaxing on 2016/12/23.
 */
import Vue from "vue";
import Modal from "../../../../../grsc-base/component/Modal.vue";
import Help from '../../../../../grsc-base/component/Help.vue';
import setLogPwd from "./modifyLogPassword.html";
import Validator from '../../../../../grsc-base/component/Validator/main';
import {REG_LOGPWD} from '../../../../../grsc-base/common/constants';
import {RULE_LOGIN_PASSWORD_LENGTH, RULE_LOGIN_PASSWORD_PATTERN} from '../../../../common/contants';
import service from '../../basic/service';
import commonService from '../../../../common/commonService';
import filter from '../../../../../grsc-base/util/filter';
import lib from '../../../../../src/common/lib'
import cookie from '../../../../../grsc-base/util/cookie';

export  default Vue.extend({
    template: setLogPwd,
    data(){
        return {
            email: '',
            form: {
                currentPwd: "",
                newPwd: "",
                confirmNewPwd: ""
            },
            rules: {
                currentPwd: {
                    required: true,
                    requiredTip: this.$t('common.currentPassRequireTip')
                },
                newPwd: {
                    tip: "",
                    required: true,
                    requiredTip: this.$t('common.newPassRequireTip'),
                    minlength: 6,
                    minlengthTip: this.$t('common.ruleLoginPassLength'),
                    maxlength: 20,
                    maxlengthTip: this.$t('common.ruleLoginPassLength'),
                    pattern: REG_LOGPWD,
                    patternTip: this.$t('common.ruleLoginPassPattern')
                },
                confirmNewPwd: {
                    required: true,
                    requiredTip: this.$t('common.repeatPassRequireTip'),
                    equalTo: 'newPwd',
                    equalToTip: this.$t('common.passEqualTip')
                }
            },
            isRequesting: false,
            googleStatus:'OFF'
        }
    },
    props: {
        onHide: {
            type: Function,
            default(){
            }
        }
    },
    computed: {},
    components: {
        'modal': Modal,
        'help': Help,
        Validator
    },
    created() {
        //this.getAccount();
        this.queryGoogleStatus();
    },
    methods: {
        queryGoogleStatus(){
            let googleBound = window.localStorage.getItem('GOOGLE_BOUND');
            let googleStatus = window.localStorage.getItem('GOOGLE_STATUS');

            if(googleBound === 'true' && googleStatus === 'ON'){
                this.googleStatus = googleStatus;
                this.form.googleCode = '';
                this.rules.googleCode = {
                    required: true,
                    requiredTip: this.$t('common.googleCodeRequireTip'),
                    pattern: '\\d{6}',
                    patternTip: window.CODE.GOOGLE_CODE[this.$i18n.locale]
                };
            }else{
                this.googleStatus = 'OFF';
            }
        },


        submit(){
            if (this.rules.$checkAll() === false) {
                return;
            }
            let param = {
                loginPassword:this.form.newPwd
            };
            let header = {
                'login-password': this.form.currentPwd
            };
            if(this.googleStatus === 'ON'){
                header['google-code'] = this.form.googleCode
            }

            this.isRequesting = true;
            service.setLogPwd(param, header).done(rep=> {

                if (rep.code === window.CODE.SUCCESS) {
                    window.success(this.$t('common.success'), () => {
                        this.hide();
                    });
                } else if (rep.code == window.CODE.LOGIN_PWD_NOT_EQUAL.CODE) {
                    window.error(window.CODE.LOGIN_PWD_NOT_EQUAL[this.$i18n.locale]);
                } else if (rep.code == window.CODE.LOGIN_PWD_FORMAT.CODE) {
                    window.error(window.CODE.LOGIN_PWD_FORMAT[this.$i18n.locale]);
                } else if (rep.code == window.CODE.LOGIN_PWD_ORIGIN.CODE && rep.data && rep.data.num) {
                    let num = rep.data ? rep.data.num : rep.date.num;
                    if(num===10){
                        window.error(window.CODE.ACCOUNT_LOCK[this.$i18n.locale]);
                        setTimeout(()=>{
                            lib.doLogout({
                                to: './login.html'
                            });
                        },3000)
                    }else{
                        // this.rules.currentPwd.$setError("Incorrect current password. " + (10 - num) + " chances left.");
                        window.error(this.$t('common.IncorrectCurrentPassTip', {num: 10 - num}));
                    }

                } else if (rep.code === window.CODE.ACCOUNT_LOCK.CODE) {
                    window.error(window.CODE.ACCOUNT_LOCK[this.$i18n.locale]);
                }else if(rep.code === window.CODE.GOOGLE_CODE.CODE){
                    window.error(window.CODE.GOOGLE_CODE[this.$i18n.locale]);
                }else {
                    window.error('原密码错误');
                }
            }).always(() => {
                this.isRequesting = false;
            })
        },
        focusTips(){
            this.rules.newPwd.tip = this.$t('common.ruleLoginPassLength');
        },
        blurTips(){
            this.rules.newPwd.tip = "";
        },
        hide(){
            this.$emit('showflag');
        },
        getAccount(){
            commonService.basic().done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.user = rep.data;
                    this.account = filter.email(this.user.account);
                }
            });
        }
    }
})